import {isNullOrWhitespace} from '@/utils'

const ACTIONS = {
    view: '查看',
    edit: '编辑',
    add: '新增',
}

export default function ({name, initForm = {}, doCreate, doDelete, doUpdate, doGetById, refresh}) {
    const modalVisible = ref(false)
    const modalAction = ref('')
    const modalTitle = computed(() => ACTIONS[modalAction.value] + name)
    const modalLoading = ref(false)
    const modalFormRef = ref(null)
    const modalForm = ref({...initForm})

    /** 新增 */
    function handleAdd() {
        modalAction.value = 'add'
        modalVisible.value = true
        modalForm.value = {...initForm}
    }

    /** 修改 */
    async function handleEdit(id) {
        modalAction.value = 'edit'
        modalVisible.value = true
        doGetById(id).then(async res => {
            modalForm.value = {...res.result}
        })

    }

    /** 查看 */
    async function handleView(id) {
        modalAction.value = 'view'
        modalVisible.value = true
        doGetById(id).then(async res => {
            modalForm.value = {...res.result}
        })
    }

    /** 保存 */
    function handleSave() {
        if (!['edit', 'add'].includes(modalAction.value)) {
            modalVisible.value = false
            return
        }
        modalFormRef.value?.validate(async (err) => {
            if (err) return
            const actions = {
                add: {
                    api: () => doCreate(modalForm.value),
                    cb: () => $message.success('新增成功'),
                },
                edit: {
                    api: () => doUpdate(modalForm.value),
                    cb: () => $message.success('编辑成功'),
                },
            }
            const action = actions[modalAction.value]

            try {
                modalLoading.value = true
                const data = await action.api()
                action.cb()
                modalLoading.value = modalVisible.value = false
                data && refresh(data)
            } catch (error) {
                modalLoading.value = false
            }
        })
    }

    /** 删除 */
    function handleDelete(id, confirmOptions) {
        if (isNullOrWhitespace(id)) return
        $dialog.confirm({
            content: '确定删除？',
            async confirm() {
                try {
                    modalLoading.value = true
                    const data = await doDelete(id)
                    $message.success('删除成功')
                    modalLoading.value = false
                    refresh(data)
                } catch (error) {
                    modalLoading.value = false
                }
            },
            ...confirmOptions,
        })
    }


    /** 发布 */
    async function handlePublish(row) {
        try {
            modalLoading.value = true
            modalForm.value = {...row}
            const data = await doUpdate(modalForm.value)
            $message.success('状态变更成功')
            modalLoading.value = false
            refresh(data)
        } catch (error) {
            modalLoading.value = false
        }
    }

    return {
        modalVisible,
        modalAction,
        modalTitle,
        modalLoading,
        handleAdd,
        handleDelete,
        handleEdit,
        handleView,
        handleSave,
        handlePublish,
        modalForm,
        modalFormRef,
    }
}
